using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Services.FileSystem;
using DotNetNuke.Services.Localization;
using DotNetNuke.UI.WebControls;

namespace Code5Systems.FormMaster.Business
{
	
	public partial class FormController
	{
		
		public CellInfo[,] GetTableMatrix(FormInfo objFormInfo, int pageNum)
		{
			FormControlInfo objFormControlInfo = default(FormControlInfo);
			CellInfo[,] tAry = null;
			int intColSpan = default(int);
			int intRowSpan = default(int);
			CellInfo oCellInfo = default(CellInfo);
			XDocument xDoc = null;
			FormPageController pageCtlr = null;

			objFormInfo.CurrentPage = Pages.Find(p => p.PageNum == pageNum);
			
			tAry = new CellInfo[objFormInfo.CurrentPage.TableRows - 1 + 1, objFormInfo.CurrentPage.TableColumns - 1 + 1];
			
			for (int row = 1; row <= objFormInfo.CurrentPage.TableRows; row++)
			{
				for (int col = 1; col <= objFormInfo.CurrentPage.TableColumns; col++)
				{
					intRowSpan = 1;
					intColSpan = 1;
					
					objFormControlInfo = GetFormControlInfo(objFormInfo, row.ToString(), col.ToString());

					if (objFormControlInfo != null && objFormControlInfo.Page == 0)
					{
						objFormControlInfo.Page = objFormInfo.CurrentPage.PageNum;
						if (xDoc == null)
						{
							xDoc = XDocument.Parse(objFormInfo.CurrentPage.PageData);
						}
						this.UpdateControlValues(xDoc, objFormControlInfo);
						objFormInfo.CurrentPage.PageData = xDoc.ToString();
						if (pageCtlr == null)
						{
							pageCtlr = new FormPageController();
						}
						pageCtlr.UpdateFormPage(objFormInfo.CurrentPage);
					}
					
					if (objFormControlInfo != null)
					{
						intRowSpan = int.Parse(objFormControlInfo.RowSpan);
						intColSpan = int.Parse(objFormControlInfo.ColSpan);
					}
					
					if (!(tAry[row - 1, col - 1] == null))
					{
						oCellInfo = tAry[row - 1, col - 1];
					}
					else
					{
						oCellInfo = null;
					}
					
					if (oCellInfo == null)
					{
						oCellInfo = new CellInfo();
						oCellInfo.Row = row;
						oCellInfo.Col = col;
						oCellInfo.RowSpan = intRowSpan;
						oCellInfo.ColSpan = intColSpan;
						oCellInfo.FormControlInfo = objFormControlInfo;
						oCellInfo.IsEmpty = oCellInfo.FormControlInfo == null;
						
						tAry[row - 1, col - 1] = oCellInfo;
						
						if (intRowSpan > 1 && intColSpan > 1)
						{
							for (int c = col + 1; c <= col + intColSpan - 1; c++)
							{
								tAry[row - 1, c - 1] = new CellInfo(true, objFormControlInfo, row, c);
							}
							
							for (int r = 0; r <= intRowSpan - 2; r++)
							{
								for (int c = 0; c <= intColSpan - 1; c++)
								{
									tAry[r + row, c + col - 1] = new CellInfo(true, objFormControlInfo, System.Convert.ToInt32((r + row) + 1), c + col);
								}
							}
						}
						else if (intColSpan > 1)
						{
							for (int c = col + 1; c <= col + intColSpan - 1; c++)
							{
								tAry[row - 1, c - 1] = new CellInfo(true, objFormControlInfo, row, c);
							}
						}
						else if (intRowSpan > 1)
						{
							for (int r = row + 1; r <= row + intRowSpan - 1; r++)
							{
								tAry[r - 1, col - 1] = new CellInfo(true, objFormControlInfo, r, col);
							}
						}
						
					}
				}
			}
			
			return tAry;
			
		}

		public void CreateForm(int moduleId, Control phForm, bool isEditMode)
		{
			FormInfo objFormInfo = default(FormInfo);
			FormSettings objFormSettings = default(FormSettings);
			Table oMainTable = new Table();
			Table oFormTable = new Table();
			TableRow oRow = default(TableRow);
			TableCell oLabelCell = null;
			TableCell oControlCell = default(TableCell);
			byte iCols = default(byte);
			byte iRows = default(byte);
			CellInfo[,] tAry = new CellInfo[2, 2];
			CellInfo oCellInfo = null;
			bool bUseLabelAsControl = default(bool);
			bool bRowVisible = true;
			string strMainTblStyle = string.Empty;
			string strFormTblStyle = string.Empty;
			int maxButtonRow = 0;
			
			FormCreationSetup();
			
			//Get the Module Settings
			objFormSettings = Utility.GetFormSettings(moduleId);

			if (objFormSettings.UseDefaultFieldFormatting)
			{
				oFormTable.Attributes.Add("class", "C5SLLC_FormItem");
			}
			
			//Get the FormInfo Object
			objFormInfo = _formInfo;
			
			//_validationGroup = (string) (Guid.NewGuid().ToString());
			
			oMainTable.ID = "C5SLLC_MainFormTable" + FormInfo.CurrentPage.PageNum.ToString();
			oMainTable.Attributes.Add("class", "c5sFormMain");
			
			if (Utility.GetPageDocType(phForm) == DocType.HTML40)
			{
				oMainTable.HorizontalAlign = objFormSettings.TableAlignment;
				if (!string.IsNullOrEmpty(objFormSettings.TableWidth))
				{
					oMainTable.Width = Unit.Parse(objFormSettings.TableWidth);
				}
				
				oFormTable.HorizontalAlign = objFormSettings.FormTableAlignment;
				if (!string.IsNullOrEmpty(objFormSettings.FormTableWidth))
				{
					oFormTable.Width = Unit.Parse(objFormSettings.FormTableWidth);
				}
				if (!string.IsNullOrEmpty(objFormSettings.FormTableHeight))
				{
					oFormTable.Height = Unit.Parse(objFormSettings.FormTableHeight);
				}
			}
			else
			{
				if (!string.IsNullOrEmpty(objFormSettings.TableWidth))
				{
					strMainTblStyle += string.Format("width: {0};", Unit.Parse(objFormSettings.TableWidth).ToString());
				}
				
				switch (objFormSettings.TableAlignment)
				{
					case HorizontalAlign.Left:
						strMainTblStyle += "margin: 0px auto 0px 0px;";
						break;
					case HorizontalAlign.Right:
						strMainTblStyle += "margin: 0px 0px 0px auto;";
						break;
					case HorizontalAlign.Center:
						strMainTblStyle += "margin: 0px auto;";
						break;
				}
				
				if (!string.IsNullOrEmpty(strMainTblStyle))
				{
					oMainTable.Attributes.Add("style", strMainTblStyle);
				}
				
				switch (objFormSettings.FormTableAlignment)
				{
					case HorizontalAlign.Left:
						strFormTblStyle += "margin: 0px auto 0px 0px;";
						break;
					case HorizontalAlign.Right:
						strFormTblStyle += "margin: 0px 0px 0px auto;";
						break;
					case HorizontalAlign.Center:
						strFormTblStyle += "margin: 0px auto;";
						break;
				}
				
				if (!string.IsNullOrEmpty(objFormSettings.FormTableWidth))
				{
					strFormTblStyle += string.Format("width: {0};", Unit.Parse(objFormSettings.FormTableWidth).ToString());
				}
				
				if (!string.IsNullOrEmpty(objFormSettings.FormTableHeight))
				{
					strFormTblStyle += string.Format("height: {0};", Unit.Parse(objFormSettings.FormTableHeight).ToString());
				}
				
				if (!string.IsNullOrEmpty(strFormTblStyle))
				{
					oFormTable.Attributes.Add("style", strFormTblStyle);
				}
				
			}
			
			if (!string.IsNullOrEmpty(objFormInfo.TableClass))
			{
				oMainTable.CssClass = objFormInfo.TableClass;
			}
			
			if (!string.IsNullOrEmpty(objFormSettings.TableClass))
			{
				oFormTable.CssClass = objFormSettings.TableClass;
			}
			
			oRow = new TableRow();
			oControlCell = new TableCell();
			oControlCell.ID = "tdFormMasterHeaderCell" + FormInfo.CurrentPage.PageNum.ToString();
			oRow.Cells.Add(oControlCell);
			oMainTable.Rows.Add(oRow);
			
			oRow = new TableRow();
			oControlCell = new TableCell();
			oControlCell.Controls.Add(oFormTable);
			oRow.Cells.Add(oControlCell);
			oMainTable.Rows.Add(oRow);
			
			oFormTable.ID = (string) (Utility.GetValidControlID(objFormInfo.FormName) + FormInfo.CurrentPage.PageNum.ToString());
			oFormTable.CellPadding = 0;
			oFormTable.CellSpacing = 0;
			
			//tAry a two dimensional Array representing Columns x Rows Matrix
			//We will keep information about what is going on in each location
			//usung CellInfo class
			tAry = GetTableMatrix(objFormInfo, objFormInfo.CurrentPage.PageNum);
			
			if (_saveSerializationFixups)
			{
				_saveSerializationFixups = false;
				DoLegacySerializationFixup();
			}
			
			for (iRows = 1; iRows <= objFormInfo.CurrentPage.TableRows; iRows++)
			{
				bRowVisible = true;
				oRow = new TableRow();
				for (iCols = 1; iCols <= objFormInfo.CurrentPage.TableColumns; iCols++)
				{
					bUseLabelAsControl = false;
					
					try
					{
						if (!(tAry[iRows - 1, iCols - 1] == null))
						{
							oCellInfo = tAry[iRows - 1, iCols - 1];
						}
						
					}
					catch (Exception)
					{
						//Do Noting
					}
					
					if (!oCellInfo.IsSpanned)
					{
						
						oControlCell = new TableCell();
						oControlCell.VerticalAlign = VerticalAlign.Top;
						oControlCell.RowSpan = oCellInfo.RowSpan;
						oControlCell.ColumnSpan = oCellInfo.ColSpan;
						
						//Label Position "2" = on the left
						if (objFormInfo.CurrentPage.LabelPosition == 2)
						{
							oLabelCell = new TableCell();
							oLabelCell.VerticalAlign = VerticalAlign.Top;
							
							if (oCellInfo.ColSpan > 1)
							{
								oControlCell.ColumnSpan = oCellInfo.ColSpan + 1;
							}
							
							oLabelCell.RowSpan = oCellInfo.RowSpan;
						}
						
						//If this is a Label or Submit Button Colspan gets weird
						//If the Control Lables are set to "2" on the Left
						//Then if the Control Lable is Hidden the ColSpan simply doubles
						//Else the ColSpan doubles minus 1 for the Control Label
						
						if (oCellInfo.FormControlInfo != null)
						{
							
							if (isEditMode)
							{
								if (oCellInfo.FormControlInfo.Type == ControlType.SubmitButton)
								{
									oCellInfo.FormControlInfo.Enabled = false;
									//Continue For
								}
							}
							
							if (oCellInfo.FormControlInfo.Type == ControlType.HiddenField)
							{
								bRowVisible = false;
							}
							
							if (oCellInfo.FormControlInfo.Type == ControlType.Label || oCellInfo.FormControlInfo.Type == ControlType.SubmitButton)
							{
								if (objFormInfo.CurrentPage.LabelPosition == 2)
								{
									if (oCellInfo.FormControlInfo.HideLabel)
									{
										oLabelCell.ColumnSpan = oCellInfo.ColSpan * 2;
										
										//If we are hiding the Control Label put the control in
										//the Label Cell
										bUseLabelAsControl = true;
									}
									else
									{
										oControlCell.ColumnSpan = (oCellInfo.ColSpan * 2) - 1;
									}
								}
							}
							
							if (oCellInfo.FormControlInfo.Type == ControlType.SubmitButton)
							{
								maxButtonRow = iRows - 1;
								if (iRows == objFormInfo.CurrentPage.TableRows)
								{
									if (objFormInfo.UseCaptcha)
									{
										//AddCaptcha(objFormInfo, oFormTable)
										//bCaptchaAdded = True
									}
								}
							}
							
							if (oLabelCell != null)
							{
								if (bUseLabelAsControl)
								{
									if (oCellInfo.FormControlInfo.ControlAlign == HorizontalAlign.NotSet)
									{
										oLabelCell.HorizontalAlign = HorizontalAlign.Left;
									}
									else
									{
										oLabelCell.HorizontalAlign = oCellInfo.FormControlInfo.ControlAlign;
									}
								}
								else
								{
									if (oCellInfo.FormControlInfo.LabelAlign == HorizontalAlign.NotSet)
									{
										oLabelCell.HorizontalAlign = HorizontalAlign.Left;
									}
									else
									{
										oLabelCell.HorizontalAlign = oCellInfo.FormControlInfo.LabelAlign;
									}
								}
							}
							
							if (oCellInfo.FormControlInfo.ControlAlign == HorizontalAlign.NotSet)
							{
								oControlCell.HorizontalAlign = HorizontalAlign.Left;
							}
							else
							{
								oControlCell.HorizontalAlign = oCellInfo.FormControlInfo.ControlAlign;
							}
							
						}

						if (bUseLabelAsControl)
						{
							AddCellControls(oCellInfo.FormControlInfo, null, oLabelCell, iRows.ToString(), iCols.ToString(), moduleId, phForm);
						}
						else
						{
							AddCellControls(oCellInfo.FormControlInfo, oLabelCell, oControlCell, iRows.ToString(), iCols.ToString(), moduleId, phForm);
						}
						
						if (oLabelCell != null)
						{
							oRow.Cells.Add(oLabelCell);
						}
						
						if (!bUseLabelAsControl)
						{
							oRow.Cells.Add(oControlCell);
						}
						
						// Get the Cascading script for this control
						if (!oCellInfo.IsEmpty)
						{
							if (oCellInfo.FormControlInfo.IsParentList || oCellInfo.FormControlInfo.IsChildList)
							{
								GetCascadedControlScript(objFormInfo.CurrentPage.PageNum, oCellInfo.FormControlInfo);
							}
						}
					}
					
				}
				
				oRow.Visible = bRowVisible;
				oFormTable.Rows.Add(oRow);
			}
			
			if (objFormInfo.ShowCcMe && (!isEditMode))
			{
				if (objFormInfo.CurrentPage.PageNum == Pages.Count)
				{
					oRow = new TableRow();
					oControlCell = new TableCell();
					
					if (objFormInfo.CurrentPage.LabelPosition == 2)
					{
						oControlCell.ColumnSpan = System.Convert.ToInt32((objFormInfo.CurrentPage.TableColumns * 2) - 1);
						oRow.Cells.Add(new TableCell());
					}
					else
					{
						oControlCell.ColumnSpan = objFormInfo.CurrentPage.TableColumns;
					}
					
					CheckBox chkCcMe = new CheckBox();
					chkCcMe.ID = "chkCcMe";
					chkCcMe.CssClass = "SubHead";
					chkCcMe.Text = Localization.GetString("ControlCcMe", this._sharedResourceFile);
					oControlCell.Controls.Add(chkCcMe);
					oRow.Cells.Add(oControlCell);
					if (maxButtonRow > 0)
					{
						oFormTable.Rows.AddAt(maxButtonRow, oRow);
					}
					else
					{
						oFormTable.Rows.Add(oRow);
					}
				}
			}
			
			if (!isEditMode)
			{
				if (objFormInfo.UseCaptcha)
				{
					switch (objFormSettings.CaptchaType)
					{
						case CaptchaType.DefaultCaptcha:
							if (objFormInfo.CurrentPage.PageNum == Pages.Count)
							{
								oRow = new TableRow();
								oRow.ID = "trC5SCaptcha";
								oControlCell = new TableCell();
								oControlCell.ID = "tdC5SCaptcha";
								oControlCell.ColumnSpan =
									System.Convert.ToInt32(objFormInfo.CurrentPage.LabelPosition == 2
										? objFormInfo.CurrentPage.TableColumns*2
										: objFormInfo.CurrentPage.TableColumns);
								oControlCell.HorizontalAlign = HorizontalAlign.Center;
								oControlCell.Attributes.Add("style", "padding:5px;display:none;");
								oControlCell.Attributes.Add("class", "C5SCaptchaCell");
								oRow.Cells.Add(oControlCell);
								if (maxButtonRow > 0)
								{
									oFormTable.Rows.AddAt(maxButtonRow, oRow);
								}
								else
								{
									oFormTable.Rows.Add(oRow);
								}
							}
							break;
						case CaptchaType.GoogleReCaptcha:
							if (objFormInfo.CurrentPage.PageNum == Pages.Count)
							{
								
								oRow = new TableRow();
								oRow.ID = "trC5SReCaptcha";
								oControlCell = new TableCell();
								oControlCell.ID = "tdC5SReCaptcha";
								oControlCell.ColumnSpan =
									System.Convert.ToInt32(objFormInfo.CurrentPage.LabelPosition == 2
										? objFormInfo.CurrentPage.TableColumns * 2
										: objFormInfo.CurrentPage.TableColumns);
								oControlCell.HorizontalAlign = HorizontalAlign.Center;
								HtmlGenericControl div = new HtmlGenericControl("div");
								div.Attributes.Add("class", "g-recaptcha");
								div.Attributes.Add("data-sitekey", objFormSettings.SiteKey);
								oControlCell.Controls.Add(div);
								oRow.Cells.Add(oControlCell);
								if (maxButtonRow > 0)
								{
									oFormTable.Rows.AddAt(maxButtonRow, oRow);
								}
								else
								{
									oFormTable.Rows.Add(oRow);
								}
							}
							break;
						default:
							break;
					}

				}
				//if (objFormInfo.UseCaptcha)
				//{
				//	if (objFormInfo.CurrentPage.PageNum == Pages.Count)
				//	{
				//		oRow = new TableRow();
				//		oRow.ID = "trC5SCaptcha";
				//		oControlCell = new TableCell();
				//		oControlCell.ID = "tdC5SCaptcha";
				//		oControlCell.ColumnSpan = System.Convert.ToInt32(objFormInfo.CurrentPage.LabelPosition == 2 ? objFormInfo.CurrentPage.TableColumns * 2 : objFormInfo.CurrentPage.TableColumns);
				//		oControlCell.HorizontalAlign = HorizontalAlign.Center;
				//		oControlCell.Attributes.Add("style", "padding:5px;display:none;");
				//		oControlCell.Attributes.Add("class", "C5SCaptchaCell");
				//		oRow.Cells.Add(oControlCell);
				//		if (maxButtonRow > 0)
				//		{
				//			oFormTable.Rows.AddAt(maxButtonRow, oRow);
				//		}
				//		else
				//		{
				//			oFormTable.Rows.Add(oRow);
				//		}
				//	}
				//}
			}
			
			if (_hasRequredFieldValidators)
			{
				if (objFormSettings.UseValidationSummary)
				{
					oRow = new TableRow();
					oControlCell = new TableCell();
					oControlCell.ColumnSpan = System.Convert.ToInt32(objFormInfo.CurrentPage.LabelPosition == 2 ? objFormInfo.CurrentPage.TableColumns * 2 : objFormInfo.CurrentPage.TableColumns);
					oControlCell.HorizontalAlign = objFormSettings.ValidationSummaryHorzAlign;
					oControlCell.VerticalAlign = VerticalAlign.Middle;
					oControlCell.Attributes.Add("style", "padding:5px;");
					
					
					ValidationSummary oValSummary = new ValidationSummary();
					oValSummary.ID = "c5svalsmry" + Utility.GetValidControlID((string) (DateTime.Now.ToString("s") + FormInfo.CurrentPage.PageNum.ToString()));
					oValSummary.ValidationGroup = objFormInfo.ValidationGroup;
					oValSummary.ShowMessageBox = objFormSettings.ShowValidationSummaryMessageBox;
					oValSummary.ShowSummary = objFormSettings.ShowValidationSummary;
					
					oValSummary.DisplayMode = objFormSettings.ValidationSummaryMode;
					oValSummary.CssClass = objFormSettings.ValidationSummaryCssClass;
					oValSummary.HeaderText = objFormSettings.ValidationSummaryHeading;
					
					oControlCell.Controls.Add(oValSummary);
					
					oRow.Cells.Add(oControlCell);
					if (objFormSettings.ValidationSummaryInsertAtRow == int.MinValue)
					{
						oFormTable.Rows.AddAt(0, oRow);
					}
					else if (objFormSettings.ValidationSummaryInsertAtRow == int.MaxValue)
					{
						oFormTable.Rows.Add(oRow);
					}
					else
					{
						if (objFormSettings.ValidationSummaryInsertAtRow > this.FormInfo.TableRows)
						{
							oFormTable.Rows.Add(oRow);
						}
						else
						{
							oFormTable.Rows.AddAt(objFormSettings.ValidationSummaryInsertAtRow, oRow);
						}
					}
				}
			}
			
			oRow = new TableRow();
			oRow.ID = "trDefaultSubmitButton" + FormInfo.CurrentPage.PageNum.ToString();
			oRow.Visible = false;
			
			oControlCell = new TableCell();
			oControlCell.ID = "tdDefaultSubmitButton" + FormInfo.CurrentPage.PageNum.ToString();
			oControlCell.ColumnSpan = System.Convert.ToInt32(objFormInfo.CurrentPage.LabelPosition == 2 ? objFormInfo.CurrentPage.TableColumns * 2 : objFormInfo.CurrentPage.TableColumns);
			oControlCell.HorizontalAlign = HorizontalAlign.Center;
			oControlCell.Attributes.Add("style", "padding:5px;");
			
			oRow.Cells.Add(oControlCell);
			
			oFormTable.Rows.Add(oRow);
			
			if (_hasRequredFieldValidators && !objFormSettings.SuppressReqFldText)
			{
				oRow = new TableRow();
				
				oControlCell = new TableCell();
				oControlCell.ColumnSpan = System.Convert.ToInt32(objFormInfo.CurrentPage.LabelPosition == 2 ? objFormInfo.CurrentPage.TableColumns * 2 : objFormInfo.CurrentPage.TableColumns);
				oControlCell.HorizontalAlign = HorizontalAlign.Center;
				oControlCell.VerticalAlign = VerticalAlign.Middle;
				oControlCell.Attributes.Add("style", "padding:5px;");
				
				Label oLabel = new Label();
				oLabel.ID = "lblRequired" + FormInfo.CurrentPage.PageNum.ToString();
				oLabel.CssClass = "normal";
				oLabel.Text = objFormSettings.RequiredFieldSymbol + Localization.GetString("Required.Text", this._sharedResourceFile);
				
				oControlCell.Controls.Add(oLabel);
				
				oRow.Cells.Add(oControlCell);
				oFormTable.Rows.Add(oRow);
			}
			
			phForm.Controls.Add(oMainTable);

			HtmlInputHidden hdnDisabledValidators = new HtmlInputHidden();
			hdnDisabledValidators.ID = Constants.DISABLED__VALIDATORS_ID;
			if (this.Pages.Count() == 1)
			{
				System.Collections.Generic.List<string> listValID = new System.Collections.Generic.List<string>();
				foreach (BaseValidator item in _disabledValidators)
				{
					listValID.Add(item.ClientID);
				}
				
				hdnDisabledValidators.Value = (string) (listValID.ToCsv());
				phForm.Controls.Add(hdnDisabledValidators);
			}

			if (FormInfo.CurrentPage.PageNum == this.Pages.Count())
			{
				if (objFormInfo.UseCaptcha)
				{
					switch (objFormSettings.CaptchaType)
					{
						case CaptchaType.DefaultCaptcha:
							this.GetCaptchaControlScript(phForm);
							break;
						case CaptchaType.GoogleReCaptcha:
							this.GetReCaptchaControlScript(phForm);
							break;
						default:
							break;
					}
					
				}
				
				if (this.Pages.Count() > 1)
				{
					this.GetWizardScripts(phForm);
				}
				
				GetFieldRuleScripts(phForm, hdnDisabledValidators);
				
				GeneratePageScripts(phForm, _pageScripts, isEditMode, DotNetNuke.Framework.AJAX.IsEnabled());
				
			}
		}
		
		//Add the control to the Design Time Form with the Error Message for any Validators
		private void AddCellControls(FormControlInfo objFormControlInfo, TableCell labelCell, TableCell controlCell, string row, string col, int moduleId, Control phForm)
		{
			FormControlInfo objValidatorFormControlInfo = default(FormControlInfo);
			Label oLabelControl = (Label) null;
			WebControl oInputControl = default(WebControl);
			BaseValidator oValidatorControl = default(BaseValidator);
			Literal oLiteral = null;
			Panel oOutsideDiv = new Panel();
			string strControlClass = default(string);
			string strTdClass = string.Empty;
			FormFieldRulesInfo fieldRule = default(FormFieldRulesInfo);
			FieldRulesScriptInfo oFieldRulesScriptInfo = null;
			bool enableValidators = true;
			string strFieldOnEventText = string.Empty;
			
			FormSettings objFormSettings = Utility.GetFormSettings(moduleId);

			if (objFormControlInfo != null)
			{
				if (this.Pages.Count > 1 && objFormControlInfo.Type == ControlType.SubmitButton)
				{
					return;
				}

				List<FieldRulesScriptInfo> sInfos = default(List<FieldRulesScriptInfo>);
				sInfos = GetAllFieldRuleScriptInfoByTarget(objFormControlInfo.ID);
				
				//Create the Outer Div Element. Panel renders as a Div
				oOutsideDiv.ID = objFormControlInfo.ID + "div";
				
				//Setup for the Field Rule With the Event Caller
				//_ruleToFind = objFormControlInfo.ID
				fieldRule = (from s in _sourceFields where s.SourceField == objFormControlInfo.ID select s).FirstOrDefault();
				//fieldRule = _sourceFields.Find(AddressOf FindFieldRule)
				//_ruleToFind = String.Empty
				
				if (fieldRule != null)
				{
					strFieldOnEventText = "return fn" + objFormControlInfo.ID + "(this,\'";
					strFieldOnEventText += objFormControlInfo.Type.ToString() + "\',true); ";
					//strFieldOnEventText &= fieldRule.ContainsValue & "'); "
				}
				
				//If the User has used a Semi-Colon in the "Class" field, get that Class Name so
				//we can apply it to the TableCell
				string[] aryCssClass = objFormControlInfo.CssClass.Split(';');
				if (aryCssClass.Length == 2)
				{
					strControlClass = (string) (aryCssClass[0].Trim());
					strTdClass = (string) (aryCssClass[1].Trim());
				}
				else
				{
					strControlClass = objFormControlInfo.CssClass.Trim();
				}

				if (!objFormControlInfo.HideLabel && objFormControlInfo.Type != ControlType.HiddenField)
				{
					if (objFormControlInfo.Type != ControlType.CheckBox)
					{
						oLabelControl = (Label) (new Label());
						oLabelControl.ID = "lbl_" + objFormControlInfo.ID;
						oLabelControl.AssociatedControlID = objFormControlInfo.ID;
						oLabelControl.CssClass = objFormControlInfo.LabelCssClass;
						if (labelCell == null)
						{
							oLabelControl.Text = objFormControlInfo.Label + (objFormControlInfo.Required ? objFormSettings.RequiredFieldSymbol : "").ToString() + "<br />";
							oOutsideDiv.Controls.Add(oLabelControl);
						}
						else
						{
							oLabelControl.Text = objFormControlInfo.Label + (objFormControlInfo.Required ? objFormSettings.RequiredFieldSymbol : "").ToString();
							
							if (strTdClass != string.Empty)
							{
								labelCell.Attributes.Add("class", strTdClass);
							}
							
							labelCell.Controls.Add(oLabelControl);
						}

						switch (objFormControlInfo.LabelAlign)
						{
							case HorizontalAlign.Left:
								oLabelControl.Attributes.Add("style", "text-align:left;display:block");
								break;
							case HorizontalAlign.Center:
								oLabelControl.Attributes.Add("style", "text-align:center;display:block");
								break;
							case HorizontalAlign.Right:
								oLabelControl.Attributes.Add("style", "text-align:right;display:block");
								break;
						}
					}
				}
				
				if (objFormControlInfo.Type == ControlType.FileUpload)
				{
					string strFileSizeFormatMsg = default(string);
					string strFileTypeFormatMsg = "";
					
					oLiteral = new Literal();
					strFileSizeFormatMsg = Localization.GetString("FileSizeFormat.Text", this._sharedResourceFile);
					
					if (strFileSizeFormatMsg == string.Empty)
					{
						strFileSizeFormatMsg = "<br /><span class=\"Normal\"><strong>File Size:</strong> [MAXFILESIZE]KB Maximum<br /><strong></span>";
					}
					strFileSizeFormatMsg = strFileSizeFormatMsg.Replace("[MAXFILESIZE]", objFormControlInfo.MaxFileSize.ToString());
					
					if (!objFormControlInfo.HideFileExtensions)
					{
						strFileTypeFormatMsg = Localization.GetString("FileTypeFormat.Text", this._sharedResourceFile);
						if (strFileTypeFormatMsg == string.Empty)
						{
							strFileTypeFormatMsg = "<br /><span class=\"Normal\"><strong>File Types:</strong> [FILETYPES]</span>";
						}
						strFileTypeFormatMsg = strFileTypeFormatMsg.Replace("[FILETYPES]", "." + objFormControlInfo.FileTypes.Replace(",", " ."));
					}
					
					oLiteral.Text = strFileSizeFormatMsg + strFileTypeFormatMsg;
					
					HtmlInputFile objHtmlControl = default(HtmlInputFile);
					objHtmlControl = new HtmlInputFile();
					objHtmlControl.ID = objFormControlInfo.ID;
					objHtmlControl.Disabled = !objFormControlInfo.Enabled;
					
					if (!HttpContext.Current.User.Identity.IsAuthenticated)
					{
						if (!objFormSettings.AllowAnonUploads)
						{
							objHtmlControl.Disabled = true;
						}
					}
					
					System.Int32 temp_result = objHtmlControl.Size;
					int.TryParse(objFormControlInfo.Width, out temp_result);
					objHtmlControl.Size = temp_result;
					//objHtmlControl.Size = CInt(objFormControlInfo.Width)
					objHtmlControl.Attributes.Add("tabindex", objFormControlInfo.TabIndex);
					
					oOutsideDiv.Controls.Add(objHtmlControl);
					
					RegularExpressionValidator revFileType = new RegularExpressionValidator();
					revFileType.ID = "revType_" + objHtmlControl.ID;
					revFileType.CssClass = "NormalRed";
					revFileType.ForeColor = System.Drawing.Color.Empty;
					revFileType.ControlToValidate = objHtmlControl.ID;
					revFileType.ErrorMessage = "<br />" + Localization.GetString("FileFormat.ErrorMessage", this._sharedResourceFile);
					revFileType.Display = ValidatorDisplay.Dynamic;
					revFileType.EnableClientScript = true;
					
					
					string strExpression = string.Empty;
					string[] aryExt = objFormControlInfo.FileTypes.Split(',');
					for (int i = 0; i <= aryExt.Length - 1; i++)
					{
						strExpression += ".+\\." + aryExt[i].ToLower() + "$|";
						strExpression += ".+\\." + aryExt[i].ToUpper() + "$";
						if (i < aryExt.Length - 1)
						{
							strExpression += "|";
						}
					}
					
					revFileType.ValidationExpression = strExpression;

					revFileType.ValidationGroup = this.FormInfo.ValidationGroup;
					
					oOutsideDiv.Controls.Add(oLiteral);
					
					oOutsideDiv.Controls.Add(revFileType);
					
				}
				else if (objFormControlInfo.Type == ControlType.HTMLEditor)
				{
					string strCtl = "<%@ Register TagPrefix=\"dnn\" TagName=\"TextEditor\" Src=\"~/controls/TextEditor.ascx\"%>";
					strCtl += Environment.NewLine;
					strCtl += string.Format("<dnn:texteditor id=\"{0}\" width=\"{1}\" height=\"{2}\" runat=\"server\" />", objFormControlInfo.ID, objFormControlInfo.Width, objFormControlInfo.Height);
					Control htmlEditor = phForm.Page.ParseControl(strCtl);
					oOutsideDiv.Controls.Add(htmlEditor);
					
				}
				else if (objFormControlInfo.Type == ControlType.HiddenField)
				{
					HtmlInputHidden oHiddenField = new HtmlInputHidden();
					if (objFormControlInfo.FieldName.ToLower() == "datarecordid")
					{
						oHiddenField.ID = objFormControlInfo.FieldName.ToLower();
					}
					else
					{
						oHiddenField.ID = objFormControlInfo.ID;
					}
					
					oHiddenField.Attributes.Add("class", objFormControlInfo.CssClass);

					oHiddenField.Attributes.Add("data-id", objFormControlInfo.CalcId);

					oHiddenField.Value = GetAutoFillValue(objFormControlInfo);

					if (!String.IsNullOrWhiteSpace(objFormControlInfo.Formula))
					{
						oHiddenField.Attributes.Add("data-formula", objFormControlInfo.Formula);
						this.HasCalculation = true;
					}

					phForm.Controls.Add(oHiddenField);
				}
				else
				{
					oInputControl = (WebControl) (GetFormControl(objFormControlInfo));

					oInputControl.Enabled = objFormControlInfo.Enabled;
					oInputControl.CssClass = strControlClass;
					oInputControl.Width = Unit.Empty;

					if (!String.IsNullOrWhiteSpace(objFormControlInfo.Width))
					{
						if (objFormControlInfo.Width.EndsWith("%"))
						{
							oInputControl.Width = Unit.Percentage(double.Parse(objFormControlInfo.Width.Replace("%", "")));
						}
						else
						{
							oInputControl.Width = Unit.Pixel(int.Parse(objFormControlInfo.Width));
						}
					}
					
					if (objFormControlInfo.Height != string.Empty)
					{
						oInputControl.Height = System.Web.UI.WebControls.Unit.Pixel(int.Parse(objFormControlInfo.Height));
					}
					
					switch (objFormControlInfo.Type)
					{
						
					case ControlType.Label:
						break;
						//oInputControl.TabIndex = -1
						
					case ControlType.CheckBox:
						C5CheckBox cBox = oInputControl as C5CheckBox;
						cBox.LabelCssClass = objFormControlInfo.LabelCssClass;
						cBox.Checked = objFormControlInfo.Checked;
						cBox.TabIndex = short.Parse(objFormControlInfo.TabIndex);
						
						cBox.CssClass = objFormControlInfo.CssClass;
						if (objFormControlInfo.HideLabel == false)
						{
							cBox.Text = objFormControlInfo.Label;
							cBox.CssClass += " " + objFormControlInfo.LabelCssClass;
						}
						
						if (!string.IsNullOrEmpty(strFieldOnEventText))
						{
							oInputControl.Attributes.Add("onclick", strFieldOnEventText);
						}
						
						bool isChecked = default(bool);
						string value = GetAutoFillValue(objFormControlInfo);
						if (!bool.TryParse(value, out isChecked))
						{
							if (String.IsNullOrWhiteSpace(value))
								value = "C5SLLC";
							cBox.Value = value;
						}
						else
						{
							if (!cBox.Checked)
							{
								cBox.Checked = isChecked;
							}
						}

						oInputControl.Attributes.Add("data-id", objFormControlInfo.CalcId);
						break;
						
						
					case ControlType.CheckBoxList:
						C5CheckBoxList chkCheckBoxList = (C5CheckBoxList)oInputControl;
						chkCheckBoxList.TabIndex = short.Parse(objFormControlInfo.TabIndex);
						chkCheckBoxList.LabelCssClass = objFormControlInfo.LabelCssClass;
						chkCheckBoxList.RepeatColumns = int.Parse(objFormControlInfo.RepeatColumns);
						chkCheckBoxList.RepeatDirection = objFormControlInfo.RepeatDirection;
						chkCheckBoxList.CellPadding = 0;
						chkCheckBoxList.CellSpacing = 0;
						chkCheckBoxList.BorderStyle = BorderStyle.None;
						ListItem lItem;
						
						if (objFormControlInfo.FillListItemText != string.Empty)
						{
							FillControlFromQuery(chkCheckBoxList, objFormControlInfo);
						}
						
						if (objFormControlInfo.AutoFillType != AutoFillType.None)
						{
							string strAutoValue = GetAutoFillValue(objFormControlInfo);
							
							foreach (var selectedValue in strAutoValue.FromCSV())
							{
								if (objFormControlInfo.ListItemToSave == ListItemToSave.Text)
								{
									lItem = chkCheckBoxList.Items.FindByText(selectedValue);
								}
								else
								{
									lItem = chkCheckBoxList.Items.FindByValue(selectedValue);
								}

								if (lItem != null)
								{
									lItem.Selected = true;
								}
							}
						}
						
						if (!string.IsNullOrEmpty(strFieldOnEventText))
						{
							chkCheckBoxList.Attributes.Add("onclick", strFieldOnEventText);
						}
						chkCheckBoxList.Attributes.Add("data-id", objFormControlInfo.CalcId);
						break;
						
					case ControlType.RadioButtonList:
						C5RadioButtonList rdoRadioButtonList = (C5RadioButtonList)oInputControl;
						rdoRadioButtonList.LabelCssClass = objFormControlInfo.LabelCssClass;
						rdoRadioButtonList.TabIndex = short.Parse(objFormControlInfo.TabIndex);
						rdoRadioButtonList.RepeatColumns = int.Parse(objFormControlInfo.RepeatColumns);
						rdoRadioButtonList.RepeatDirection = objFormControlInfo.RepeatDirection;
						rdoRadioButtonList.CellPadding = 0;
						rdoRadioButtonList.CellSpacing = 0;
						rdoRadioButtonList.BorderStyle = BorderStyle.None;
						
						if (objFormControlInfo.FillListItemText != string.Empty)
						{
							FillControlFromQuery(rdoRadioButtonList, objFormControlInfo);
						}
						
						if (objFormControlInfo.AutoFillType != AutoFillType.None)
						{
							string strAutoValue = GetAutoFillValue(objFormControlInfo);
							rdoRadioButtonList.SelectedIndex = rdoRadioButtonList.Items.IndexOf(rdoRadioButtonList.Items.FindByText(strAutoValue));
						}
						
						if (!string.IsNullOrEmpty(strFieldOnEventText))
						{
							rdoRadioButtonList.Attributes.Add("onclick", strFieldOnEventText);
						}
						rdoRadioButtonList.Attributes.Add("data-id", objFormControlInfo.CalcId);
						break;
						
						
					case ControlType.TextBox:
						if (objFormControlInfo.UseCalendar)
						{
							C5DatePicker picker = new C5DatePicker();
							picker.ID = objFormControlInfo.ID;
							picker.MinDate = DateTime.Now.AddYears(-80);
							DateTime autoFilldate;
							if (DateTime.TryParse(GetAutoFillValue(objFormControlInfo), out autoFilldate))
							{
								picker.SelectedDate = autoFilldate;
							}
							oInputControl = picker;
							oInputControl.Attributes.Add("type", "date");
							if (!String.IsNullOrWhiteSpace(objFormControlInfo.Placeholder))
							{
								oInputControl.Attributes.Add("placeholder", objFormControlInfo.Placeholder);
							}
						}
						else
						{
							((TextBox)oInputControl).Text = GetAutoFillValue(objFormControlInfo);
							if (objFormControlInfo.Columns != string.Empty)
							{
								((TextBox)oInputControl).Columns = int.Parse(objFormControlInfo.Columns);
							}
							if (objFormControlInfo.TextMode == TextBoxMode.MultiLine)
							{
								((TextBox)oInputControl).Wrap = objFormControlInfo.WrapHard;

								if (objFormControlInfo.Rows != string.Empty)
								{
									((TextBox)oInputControl).Rows = int.Parse(objFormControlInfo.Rows);
								}
							}

							if (!String.IsNullOrWhiteSpace(objFormControlInfo.Placeholder))
							{
								oInputControl.Attributes.Add("placeholder", objFormControlInfo.Placeholder);
							}

							oInputControl.Attributes.Add("data-id", objFormControlInfo.CalcId);
						}

						oInputControl.TabIndex = short.Parse(objFormControlInfo.TabIndex);

						if (!string.IsNullOrEmpty(strFieldOnEventText))
						{
							oInputControl.Attributes.Add("onblur", strFieldOnEventText);
						}

						if (!String.IsNullOrWhiteSpace(objFormControlInfo.Formula))
						{
							oInputControl.Attributes.Add("data-formula", objFormControlInfo.Formula);
							this.HasCalculation = true;
						}
						
						break;
						
					case ControlType.ListBox:
						oInputControl.TabIndex = short.Parse(objFormControlInfo.TabIndex);
						ListBox lstListBox = (ListBox) oInputControl;
						lstListBox.SelectionMode = objFormControlInfo.SelectionMode;
						
						if (objFormControlInfo.FillListItemText != string.Empty)
						{
							FillControlFromQuery(lstListBox, objFormControlInfo);
						}
						
						if (objFormControlInfo.AutoFillType != AutoFillType.None)
						{
							string strAutoValue = GetAutoFillValue(objFormControlInfo);
							lstListBox.SelectedIndex = lstListBox.Items.IndexOf(lstListBox.Items.FindByText(strAutoValue));
						}
						
						oLiteral = new Literal();
						oLiteral.Text = Localization.GetString("MultiSelect.Text", Utility.GetSharedResourceFile());
						
						if (!string.IsNullOrEmpty(strFieldOnEventText))
						{
							lstListBox.Attributes.Add("onchange", strFieldOnEventText);
						}
						oInputControl.Attributes.Add("data-id", objFormControlInfo.CalcId);
						break;
						
					case ControlType.DropDownList:
						oInputControl.TabIndex = short.Parse(objFormControlInfo.TabIndex);
						DropDownList lstDropDown = (DropDownList) oInputControl;
						
						if (objFormControlInfo.FillListItemText != string.Empty)
						{
							FillControlFromQuery(lstDropDown, objFormControlInfo);
						}
						
						if (objFormControlInfo.AutoFillType != AutoFillType.None)
						{
							string strAutoValue = GetAutoFillValue(objFormControlInfo);
							if (objFormControlInfo.ListItemToSave == ListItemToSave.Text)
							{
								lstDropDown.SelectedIndex = lstDropDown.Items.IndexOf(lstDropDown.Items.FindByText(strAutoValue));
							}
							else
							{
								lstDropDown.SelectedIndex = lstDropDown.Items.IndexOf(lstDropDown.Items.FindByValue(strAutoValue));
							}
							
						}
						
						if (!string.IsNullOrEmpty(strFieldOnEventText))
						{
							lstDropDown.Attributes.Add("onchange", strFieldOnEventText);
						}
						oInputControl.Attributes.Add("data-id", objFormControlInfo.CalcId);
						break;
						
					case ControlType.SubmitButton:
						oInputControl.TabIndex = short.Parse(objFormControlInfo.TabIndex);
						switch (objFormControlInfo.ButtonType)
						{
							case Code5Systems.FormMaster.Business.ButtonType.Button:
								((Button) oInputControl).Text = objFormControlInfo.Label;
								((Button)oInputControl).ValidationGroup = this.FormInfo.ValidationGroup;
								break;

							case Code5Systems.FormMaster.Business.ButtonType.LinkButton:
								((LinkButton) oInputControl).Text = objFormControlInfo.Label;
								((LinkButton)oInputControl).ValidationGroup = this.FormInfo.ValidationGroup;
								break;

							case Code5Systems.FormMaster.Business.ButtonType.ImageButton:
								PortalSettings _portalSettings = DotNetNuke.Entities.Portals.PortalController.GetCurrentPortalSettings();
								
								int intVal;
								int.TryParse(objFormControlInfo.ButtonImage, out intVal);
								DotNetNuke.Services.FileSystem.FileInfo oFileInfo = FileManager.Instance.GetFile(intVal) as DotNetNuke.Services.FileSystem.FileInfo;
								
								if (oFileInfo != null)
								{
									((ImageButton) oInputControl).ImageUrl = _portalSettings.HomeDirectory + oFileInfo.Folder + oFileInfo.FileName;
								}
								
								((ImageButton) oInputControl).AlternateText = objFormControlInfo.Label;

								((ImageButton)oInputControl).ValidationGroup = this.FormInfo.ValidationGroup;
								break;
								
						}
						this.FormButtons.Add(oInputControl);
						break;
						
				}
				
				if (oInputControl != null)
				{
					oOutsideDiv.Controls.Add(oInputControl);
				}
				
				
				if (oLiteral != null)
				{
					oOutsideDiv.Controls.Add(oLiteral);
				}
			}
			
			//If the Control is a Target of any Field Rules, add it to the collection for later processing
			if (_targetFields.Contains(objFormControlInfo.ID))
			{
				
				foreach (FieldRulesScriptInfo tempLoopVar_oFieldRulesScriptInfo in sInfos)
				{
					oFieldRulesScriptInfo = tempLoopVar_oFieldRulesScriptInfo;
					if (oFieldRulesScriptInfo != null)
					{
						//Add the FormControlInfo for the Target Field
						oFieldRulesScriptInfo.TargetControlInfos.Add(objFormControlInfo.ID, objFormControlInfo);
						
						//Add the Panel Control for the Target Field
						oFieldRulesScriptInfo.TargetControls.Add(objFormControlInfo.ID, oOutsideDiv);
						
						//Add the Label Control for the Target Field
						if (oLabelControl != null)
						{
							oFieldRulesScriptInfo.TargetLabels.Add(oLabelControl);
						}
						
					}
				}
				
				//Get the Source Field for this Target Fiels
				FormFieldRulesInfo sourceField = GetSourceFieldByTarget(objFormControlInfo.ID);
				
				if (sourceField != null)
				{
					string fillValue = GetAutoFillValue(objFormControlInfo);
					
					FormControlInfo sourceCtlInfo = this.GetFormControlInfo(sourceField.SourceField);
					
					if (sourceCtlInfo != null)
					{
						
						switch (sourceCtlInfo.Type)
						{
							case ControlType.CheckBox:
								if (sourceCtlInfo.Checked)
								{
									fillValue = "Checked";
								}
								break;
							case ControlType.RadioButtonList:
								C5RadioButtonList rdoCtrl = (C5RadioButtonList)(GetFormControl(sourceCtlInfo));
								fillValue = GetAutoFillValue(sourceCtlInfo);
								if (string.IsNullOrEmpty(fillValue))
								{
									fillValue = rdoCtrl.SelectedValue;
								}
								break;
							case ControlType.DropDownList:
								DropDownList lstCtl_1 = (DropDownList) (GetFormControl(sourceCtlInfo));
								fillValue = GetAutoFillValue(sourceCtlInfo);
								if (string.IsNullOrEmpty(fillValue))
								{
									fillValue = lstCtl_1.SelectedValue;
								}
								break;
							case ControlType.ListBox:
								ListBox lstCtl = (ListBox) (GetFormControl(sourceCtlInfo));
								foreach (ListItem item in lstCtl.Items)
								{
									if (item.Selected)
									{
										fillValue += item.Value + ",";
									}
								}
								break;
							case ControlType.CheckBoxList:
								fillValue = string.Empty;
								C5CheckBoxList chkLst = (C5CheckBoxList)(GetFormControl(sourceCtlInfo));
								foreach (ListItem item in chkLst.Items)
								{
									if (item.Selected)
									{
										fillValue += item.Value + ",";
									}
								}
								break;
							default:
								fillValue = GetAutoFillValue(sourceCtlInfo);
								break;
						}
					}
					
					string strDisplay = string.Empty;
					
					if (sourceField.ContainsValue == "*.*")
					{
						if (!string.IsNullOrEmpty(fillValue))
						{
							strDisplay = "display:block;";
							enableValidators = true;
						}
						else
						{
							strDisplay = "display:none;";
							enableValidators = false;
						}
						oOutsideDiv.Attributes.Add("style", strDisplay);
						if (oLabelControl != null)
						{
							oLabelControl.Attributes.Add("style", strDisplay);
						}
					}
					else if (!fillValue.Contains(sourceField.ContainsValue))
					{
						oOutsideDiv.Attributes.Add("style", "display:none;");
						if (oLabelControl != null)
						{
							oLabelControl.Attributes.Add("style", "display:none;");
						}
						enableValidators = false;
					}
					
				}
			}
			
			if (strTdClass != string.Empty)
			{
				controlCell.Attributes.Add("class", strTdClass);
			}
			
			if (objFormControlInfo.Validators.Count > 0)
			{
				this.HasValidators = true;
				System.Collections.IEnumerator myEnumerator = objFormControlInfo.Validators.GetEnumerator();
				while (myEnumerator.MoveNext())
				{
					
					objValidatorFormControlInfo = (FormControlInfo) myEnumerator.Current;
					
					oValidatorControl = (BaseValidator) (GetFormControl(objValidatorFormControlInfo));

					oValidatorControl.Attributes.Add("data-c5val", "0");
					
					oValidatorControl.EnableClientScript = objFormSettings.EnableClientScript;

					oValidatorControl.ValidationGroup = this.FormInfo.ValidationGroup;
					
					oValidatorControl.Enabled = enableValidators;
					
					oValidatorControl.SetFocusOnError = objValidatorFormControlInfo.SetFocusOnError;
					
					if (!oValidatorControl.Enabled)
					{
						if (!_disabledValidators.Contains(oValidatorControl))
						{
							_disabledValidators.Add(oValidatorControl);
						}
					}

					if (objFormSettings.SupressFieldValidationMessages)
					{
						oOutsideDiv.Controls.Add(oValidatorControl);
					}
					else
					{
						HtmlGenericControl valDiv = new HtmlGenericControl("div");
						valDiv.Controls.Add(oValidatorControl);
						if (objFormSettings.UseFormErrorFormatting)
						{
							oOutsideDiv.Controls.AddAt(0, valDiv);
						}
						else
						{
							oOutsideDiv.Controls.Add(valDiv);
						}
					}
					
					if (objValidatorFormControlInfo.Type == ControlType.RequiredFieldValidator)
					{
						_hasRequredFieldValidators = true;
					}
					
					foreach (FieldRulesScriptInfo tempLoopVar_oFieldRulesScriptInfo in sInfos)
					{
						oFieldRulesScriptInfo = tempLoopVar_oFieldRulesScriptInfo;
						oFieldRulesScriptInfo.TargetValidators.Add(oValidatorControl);
					}
				}
			}
		}
		
		controlCell.Controls.Add(oOutsideDiv);
		
		return;
		
	}
	
	private void AddCaptcha(FormInfo oFormInfo, Table oTable)
	{
		TableRow oRow = new TableRow();
		oRow.ID = "trCaptchaRow_" + oFormInfo.ModuleID.ToString();
		oRow.Visible = true;
		
		TableCell oControlCell = new TableCell();
		oControlCell.ID = "tdCapthcaCell_" + oFormInfo.ModuleID.ToString();
		oControlCell.ColumnSpan = System.Convert.ToInt32(oFormInfo.CurrentPage.LabelPosition == 2 ? oFormInfo.TableColumns * 2 : oFormInfo.TableColumns);
		oControlCell.HorizontalAlign = HorizontalAlign.Center;
		oControlCell.Attributes.Add("style", "padding:5px;");
		
		CaptchaControl ctlCaptcha = new CaptchaControl();
		ctlCaptcha.ID = "cpt_FormMaster_" + oFormInfo.ModuleID.ToString();
		ctlCaptcha.TabIndex = (short) (GetControlCount(oFormInfo) + 1);
		ctlCaptcha.CaptchaWidth = Unit.Pixel(130);
		ctlCaptcha.CaptchaChars = System.Convert.ToString((oFormInfo.CaptchaCharacters == string.Empty ? (Localization.GetString("CaptchaCharaters", this.SharedResouceFile)) : oFormInfo.CaptchaCharacters).ToString());
		ctlCaptcha.CaptchaHeight = Unit.Pixel(40);
		ctlCaptcha.Text = Localization.GetString("CaptchaText", this.SharedResouceFile);
		ctlCaptcha.CssClass = "Normal";
		ctlCaptcha.ErrorStyle.CssClass = "NormalRed";
		ctlCaptcha.ErrorMessage = Localization.GetString("CaptchaErrorMsg", Utility.GetSharedResourceFile());
		
		oControlCell.Controls.Add(ctlCaptcha);
		oRow.Cells.Add(oControlCell);
		oTable.Rows.Add(oRow);
	}
	
}

}

